﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
#if LIVE
using Microsoft.Xna.Framework.GamerServices;
#endif
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
#if WINDOWS_PHONE
using Microsoft.Xna.Framework.Input.Touch;
#endif
#if WINDOWS
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
#endif
using Microsoft.Xna.Framework.Media;
using Neat;
using Neat.MenuSystem;
using Neat.EasyMenus;
using Neat.GUI;
using Neat.Mathematics;
using Neat.Graphics;
using System.IO;

namespace Sectors
{
    public class SectorsScreen : Screen
    {
        public World World;
        public bool ShowLogo = true;
        public SectorsScreen(NeatGame Game, World w)
            : base(Game)
        {
            World = w;
        }
        public SectorsScreen(NeatGame Game)
            : base(Game)
        {
        }

        public override void Initialize()
        {
            game.Graphics.PreferMultiSampling = true;
            base.Initialize();
        }

        public override void Activate()
        {
            base.Activate();
        }

        public override void Behave(GameTime gameTime)
        {
            base.Behave(gameTime);
        }

        LineSegment player;
        RenderTarget2D rt;
        public override void LoadContent()
        {
            game.LoadTexture(@"Sprites\sectorslogo");

            game.LoadTexture(@"Sprites\cow", "generic_floor");
            game.LoadTexture(@"Sprites\grad", "generic_ceiling");
            game.LoadTexture(@"Sprites\Doomguy1", "generic_wall");
            game.LoadTexture(@"Sprites\CementWall", "cement_wall");
            game.LoadTexture(@"Sprites\ColorfulFloor", "colorful_floor");
            game.LoadTexture(@"Sprites\WhiteCeiling", "white_ceiling");
            game.LoadTexture(@"Sprites\lava_green");
            game.LoadTexture(@"Sprites\metal");
            game.LoadTexture(@"Sprites\icon");

            if (File.Exists("textures.lst"))
            {
                using (StreamReader sr = new StreamReader("textures.lst"))
                {
                    while (!sr.EndOfStream)
                    {
                        string[] pair = sr.ReadLine().Split(' ');
                        game.LoadTexture(pair[1], pair[0]);
                    }
                }
            }

            if (World != null)
            {
                game.Console.AddCommand("g_reset", g_reset2);
                World = new World(game, World);
            }
            else
            {
                game.Console.AddCommand("g_reset", g_reset);
                World = new World(game);
            }
            game.Console.AddCommand("g_showlogo", g_showlogo);
            rt = new RenderTarget2D(
                game.GraphicsDevice, game.GameWidth,
                game.Console.MeasureHeight(game.Console.LinesCount));

            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            World.Update(gameTime);
            base.Update(gameTime);
        }

        public override void HandleInput(GameTime gameTime)
        {
            base.HandleInput(gameTime);
        }

        
        public override void Render(GameTime gameTime)
        {
            //game.GraphicsDevice.SetRenderTarget(rt);
            //game.SpriteBatch.Begin();
            //game.Console.Draw(false);
            //game.SpriteBatch.End();
            //game.GraphicsDevice.SetRenderTarget(null);
            //game.AssignTexture(rt, "console");
            World.Render(gameTime);
            if (ShowLogo)
            {
                //game.SpriteBatch.Begin();
                var logo = game.GetTexture("sectorslogo");
                game.SpriteBatch.Draw(logo, new Vector2(game.GameWidth - logo.Width - 10, game.GameHeight - logo.Height - 10), GraphicsHelper.GetColorWithAlpha(Color.White, 0.6f));
                //game.SpriteBatch.End();
            }
            base.Render(gameTime);
        }

        #region Console
        void g_reset(IList<string> args)
        {
            World = new World(game);
        }
        void g_reset2(IList<string> args)
        {
            World = new World(game, World);
        }
        void g_showlogo(IList<string> args)
        {
            ShowLogo = bool.Parse(args[1]);
        }
        #endregion
    }
}
